package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-11-02
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {

    //根据名称模糊查询分页列表
    public ResponseResult listByName(ChannelDto dto) {
        //构造条件
        LambdaQueryWrapper<WmChannel> qw=new LambdaQueryWrapper<>();
        //判断传入的参数是否为空,不为空添加查询条件
        if (!StringUtils.isEmpty(dto.getName())) {
            qw.like(WmChannel::getName,dto.getName());
        }
        //构造分页条件
        Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<WmChannel> iPage = this.page(page, qw);
        //返回分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
        return result;
    }

    //新增频道
    public ResponseResult saveChannel(WmChannel channel) {
        //判断参数
        if (channel==null || StringUtils.isEmpty(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据频道名称查询表记录
        LambdaQueryWrapper<WmChannel> qw=new LambdaQueryWrapper<>();
        qw.eq(WmChannel::getName,channel.getName());
        int count = this.count(qw);
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 不存在,保存到表中
        // 添加当前时间
        channel.setCreatedTime(new Date());
        this.save(channel);
        // 返回频道ID
        return ResponseResult.okResult(channel.getId());
    }

    //修改
    public ResponseResult updateChannel(WmChannel channel) {
        //判断参数
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查找数据
        LambdaQueryWrapper<WmChannel> qw=new LambdaQueryWrapper<>();
        qw.eq(WmChannel::getId,channel.getId());
        WmChannel one = this.getOne(qw);
        if (one==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改
        boolean b = this.updateById(channel);
        if (!b) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(b);
    }

    //删除
    public ResponseResult deleteChannel(Long channelId) {
        //判断参数
        if (StringUtils.isEmpty(channelId)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查找数据
        LambdaQueryWrapper<WmChannel> qw=new LambdaQueryWrapper<>();
        qw.eq(WmChannel::getId,channelId);
        WmChannel one = this.getOne(qw);
        if (one==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //删除
        boolean b = this.removeById(channelId);
        if (!b) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(b);
    }
}
